package com.wang.API;

import com.wang.bean.*;
import com.wang.service.TestService;
import com.wang.util.RedisUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("api")
public class TestController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private TestService testService;

    @RequestMapping(value = "/setTestPaperNumber")
    @RequiresPermissions("sys:task:setTestPaperNumber")
    public Result setTestPaperNumber(@RequestBody ParameterNum pnum){
        Result result = new Result();
        Integer num = pnum.getNum();
        String examination_name = pnum.getExamination_name();
        if (num == null || num <= 0){
            result.setCode("901");
            return result;
        }
        if (examination_name == null || "".equals(examination_name)){
            result.setCode("906");
            return result;
        }
        List<TestPaper> randbynumber = testService.randbynumber(num);
        List<TestPaper> testPapers = new ArrayList<>();
        String examination = UUID.randomUUID().toString();
        for (TestPaper testPaper : randbynumber){
            testPaper.setExamination_id(examination);
            testPaper.setExamination_name(examination_name);
            testService.addTestPaper(testPaper);
            testPapers.add(testPaper);
        }
        result.setCode("902");
        result.setMessage("考试题目设置成功，成功总数为：" + randbynumber.size() + "！");
        result.setData(testPapers.get(0).getExamination_id());
        return result;
    }

    @RequestMapping(value = "/setTestPaperChoose")
    @RequiresPermissions("sys:task:setTestPaperChoose")
    public Result setTestPaperChoose(@RequestBody ParameterIds pids){
        Result result = new Result();
        List<Long> ids = pids.getIds();
        String examination_name = pids.getExamination_name();
        if (ids == null){
            result.setCode("905");
            return result;
        }
        if (ids.size() == 0){
            result.setCode("905");
            return result;
        }
        List<TestPaper> testPapers = new ArrayList<>();
        String message = "考试题目设置成功";
        String examination = UUID.randomUUID().toString();
        for (Long id : ids){
            TestPaper testPaper = testService.selectoneTestPaper(id);
            if (testPaper == null){
                message += "，id：" + id + "设置失败";
                continue;
            }
            System.out.println(testPaper);
            testPaper.setExamination_id(examination);
            testPaper.setExamination_name(examination_name);
            testService.addTestPaper(testPaper);
            testPapers.add(testPaper);
        }
        if (testPapers.size() == 0){
            result.setCode("908");
            return result;
        }
        message += "，成功总数为：" + testPapers.size() + "！";
        result.setCode("902");
        result.setMessage(message);
        result.setData(testPapers.get(0).getExamination_id());
        return result;
    }

    @RequestMapping(value = "/getTestPaper")
    @RequiresPermissions("sys:task:getTestPaper")
    public Result getTestPaper(@RequestBody String examination_id){
        Result result = new Result();
        if (examination_id == null || "".equals(examination_id)){
            result.setCode("907");
            return result;
        }
        System.out.println(examination_id);
        List<TestPaper> testPapers = testService.findTestPaperByExaminationId(examination_id);
        if (testPapers == null){
            result.setCode("903");
            return result;
        }
        if (testPapers.size() == 0){
            result.setCode("903");
            return result;
        }
        result.setCode("904");
        result.setData(testPapers);
        return result;
    }

}
